package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.OmsCycleType;
import com.sg.service.base.app.MOmsCycleTypeService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsCycleTypeMapper;
import com.wicket.okrframework.common.annotations.BanAuto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsCycleTypeServiceImpl
        extends ServiceImpl<OmsCycleTypeMapper, OmsCycleType>
        implements MOmsCycleTypeService {

    @Autowired
    OmsCycleTypeMapper omsCycleTypeMapper;

    /**
     * 3-4-01批量改周期类型[2170]
     * gen by moon at 10/14/2022, 10:50:08 PM
     */
    @Trace(operationName = "3-4-01批量改周期类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateCycleType(List<OmsCycleType> omsCycleType) {

        if (omsCycleType == null || omsCycleType.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsCycleType);

    }

    /**
     * 3-4-01查周期类型详情[1881]
     * gen by moon at 4/7/2023, 6:58:31 PM
     */
    @Trace(operationName = "3-4-01查周期类型详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsCycleType queryCycleTypeDetail(QueryCycleTypeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsCycleType> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsCycleType> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsCycleType retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-01删除周期类型[1852]
     * gen by moon at 10/14/2022, 10:50:07 PM
     */
    @Trace(operationName = "3-4-01删除周期类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCycleType(String cycleTypeId) {

        return super.removeById(cycleTypeId);
    }

    /**
     * 3-4-01查周期类型列表[1851]
     * gen by moon at 10/14/2022, 10:50:07 PM
     */
    @Trace(operationName = "3-4-01查周期类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleType> queryCycleTypeList(QueryCycleTypeListReq reqDto) {

        try {
            QueryWrapper<OmsCycleType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsCycleType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-01修改周期类型[1850]
     * gen by moon at 10/14/2022, 10:50:06 PM
     */
    @Trace(operationName = "3-4-01修改周期类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCycleType(OmsCycleType omsCycleType) {
        omsCycleType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsCycleType != null && omsCycleType.getSubjectLifeCycle() != null && omsCycleType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsCycleType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsCycleType != null && omsCycleType.getSubjectLifeCycle() != null && omsCycleType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(omsCycleType);
    }

    /**
     * 3-4-01新增周期类型[1849]
     * gen by moon at 10/14/2022, 10:50:05 PM
     */
    @Trace(operationName = "3-4-01新增周期类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @BanAuto
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCycleType(OmsCycleType omsCycleType) {
        omsCycleType.setCycleTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsCycleType.setIsArchive("FALSE");
        omsCycleType.setCycleTypeNumber("CT" + CommonFunctionHelper.getUid());
        omsCycleType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleType.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsCycleType != null && omsCycleType.getSubjectLifeCycle() != null && omsCycleType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsCycleType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsCycleType != null && omsCycleType.getSubjectLifeCycle() != null && omsCycleType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsCycleType.setIsValid("FALSE");
        omsCycleType.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsCycleType.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsCycleType)) {
            return omsCycleType.getCycleTypeId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-4-01批量查周期类型列表[2274]
     * gen by moon at 11/27/2022, 9:19:50 PM
     */
    @Trace(operationName = "3-4-01批量查周期类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleType> batchQueryCycleTypeList(BatchQueryCycleTypeListReq reqDto) {

        try {
            QueryWrapper<OmsCycleType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getCycleTypeList() == null || reqDto.getCycleTypeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("cycle_type_id", reqDto.getCycleTypeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsCycleType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-01新增租户周期类型[5067]
     * gen by moon at 10/30/2022, 1:44:35 AM
     */
    @Trace(operationName = "3-4-01新增租户周期类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSpaceCycleType(OmsCycleType omsCycleType) {
        omsCycleType.setCycleTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsCycleType.setSubjectLifeCycle("EDITING");
        omsCycleType.setIsArchive("FALSE");
        omsCycleType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleType.setIsValid("FALSE");
        if (super.save(omsCycleType)) {
            return omsCycleType.getCycleTypeId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-4-01新增自定义周期类型[5067]
     * gen by moon at 10/31/2022, 9:15:47 PM
     */
    @Trace(operationName = "3-4-01新增自定义周期类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCustomCycleType(OmsCycleType omsCycleType) {
        omsCycleType.setCycleTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsCycleType.setCycleTypeNumber(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsCycleType.setIsArchive("FALSE");
        omsCycleType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleType.setIsValid("FALSE");
        if (super.save(omsCycleType)) {
            return omsCycleType.getCycleTypeId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-4-02批量查询租户周期类型byCode[5291]
     * gen by moon at 11/26/2022, 6:17:18 PM
     */
    @Trace(operationName = "3-4-02批量查询租户周期类型byCode")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleType> batchQueryTenantCycleType(BatchQueryTenantCycleTypeReq reqDto) {

        try {
            QueryWrapper<OmsCycleType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getTenantCycleTypeList() == null || reqDto.getTenantCycleTypeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("cycle_type_code", reqDto.getTenantCycleTypeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsCycleType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-01查询租户节假日菜单列表[5311]
     * gen by moon at 11/29/2022, 11:00:11 AM
     */
    @Trace(operationName = "3-4-01查询租户节假日菜单列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleType> queryTenantHolidayMenuList(QueryTenantHolidayMenuListReq reqDto) {

        try {
            QueryWrapper<OmsCycleType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsCycleType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
